/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;

import beans.Bean;
import beans.ObraSocialBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Cristian Ketibian
 * @version 1.0
 */
public class MSSQLObraSocialDao extends MSSQLDao implements Dao{
    
     public MSSQLObraSocialDao() throws Exception {
        super();
    }

     @Override
    public Bean make(ResultSet result) throws Exception {
        ObraSocialBean os= new ObraSocialBean();
        os.setId_os(result.getInt("id_obra_social"));
        os.setNombre(result.getString("nombre")); 
        os.setAccesoWS(result.getString("acceso_ws"));          
        os.setCalle(result.getString("calle"));
        os.setNumero(result.getString("nro"));
        os.setPiso(result.getString("piso"));
        os.setDepto(result.getString("dpto"));
        os.setBarrio(result.getString("barrio"));
        os.setCiudad(result.getString("ciudad"));
        os.setProvincia(result.getString("provincia"));
        os.setPais(result.getString("pais"));
        os.setEmail(result.getString("email_os"));
        os.setPaginaWeb(result.getString("pagina_web"));
        os.setTelefono(result.getString("telefono"));
        
        
        return os;
    }


    @Override
    public long insert(Bean bean) throws Exception {
      long rs = -1;
         try {
            this.getConnection().setAutoCommit(false);//para mantener la integridad de la base, usar transaccion            
            this.setStatement("ins_obraSocial(?,?)");
            this.getStatement().setString(1, ((ObraSocialBean) bean).getNombre()); 
            this.getStatement().setString(2, ((ObraSocialBean) bean).getAccesoWS());  
            if (this.getStatement().executeUpdate() == 1) {
                this.getConnection().commit();
                rs = 1;//this.get(bean);
            }
            
        } catch (SQLException ex) {
            this.getConnection().rollback();
            rs = -2;
        } catch (Exception ex2) { 
            this.getConnection().rollback();
            rs = -3;
        }        
        finally {
            this.getConnection().setAutoCommit(true);
            this.destroy();
        }
        return rs;    
    }

     @Override
    public void update(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

     @Override
    public List<Bean> select(Bean bean) throws Exception {
        ResultSet result;
        List<Bean> resultados = new ArrayList<Bean>();
        this.setStatement("get_os");   
        
        result = this.getStatement().executeQuery();       
        while(result.next()) {
            resultados.add(this.make(result));
        }        
        return resultados;  
    }

     @Override
    public boolean valid(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Bean get(Bean bean) throws Exception {
        try{                     
            this.setStatement("get_obra_social(?)"); 
            this.getStatement().setString(1, ((ObraSocialBean) bean).getNombre()); 
            ResultSet result = this.getStatement().executeQuery();      
            result.next();              
            bean = make(result);  
             
         } catch (SQLException ex) {            
            return null;
        } catch (Exception ex2) { throw ex2; }  
        finally { this.destroy(); }
        return bean;
    }  

    @Override
    public long getSize(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
